/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.crab.entity.CrabProduct;
import com.kaaction.modules.crab.service.CrabProductService;

/**
 * 商品管理Controller
 * @author huanghaiming
 * @version 2017-07-18
 */
@Controller
@RequestMapping(value = "${adminPath}/crab/crabProduct")
public class CrabProductController extends BaseController {

	@Autowired
	private CrabProductService crabProductService;
	
	@ModelAttribute
	public CrabProduct get(@RequestParam(required=false) String id) {
		CrabProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crabProductService.get(id);
		}
		if (entity == null){
			entity = new CrabProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("crab:crabProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(CrabProduct crabProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CrabProduct> page = crabProductService.findPage(new Page<CrabProduct>(request, response), crabProduct); 
		model.addAttribute("page", page);
		return "modules/crab/crabProductList";
	}

	@RequiresPermissions("crab:crabProduct:form")
	@RequestMapping(value = "form")
	public String form(CrabProduct crabProduct, Model model) {
		model.addAttribute("crabProduct", crabProduct);
		return "modules/crab/crabProductForm";
	}

	@RequiresPermissions("crab:crabProduct:save")
	@RequestMapping(value = "save")
	public String save(CrabProduct crabProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, crabProduct)){
			return form(crabProduct, model);
		}
		crabProductService.save(crabProduct);
		addMessage(redirectAttributes, "保存商品管理成功");
		return "redirect:"+Global.getAdminPath()+"/crab/crabProduct/?repage";
	}
	
	@RequiresPermissions("crab:crabProduct:delete")
	@RequestMapping(value = "delete")
	public String delete(CrabProduct crabProduct, RedirectAttributes redirectAttributes) {
		crabProductService.delete(crabProduct);
		addMessage(redirectAttributes, "删除商品管理成功");
		return "redirect:"+Global.getAdminPath()+"/crab/crabProduct/?repage";
	}
	
	@RequiresPermissions("crab:crabProduct:save")
	@RequestMapping(value = "updateIsShow")
	public String updateIsShow(CrabProduct crabProduct, RedirectAttributes redirectAttributes) {
		crabProductService.updateIsShow(crabProduct);
		return "redirect:"+Global.getAdminPath()+"/crab/crabProduct/?repage";
	}

}